Shader "Dan/UI/UI_Sphere"
{
    Properties
    {
        [HDR]_MainColor("_MainColor",Color ) =(0,0.3,0.7,1)
        _MapTex("MapTex", 2D) = "white" {}
        _MotionTex("Motion Tex",2D ) ="white"{}

        _FlowSpeed("Flow Speed", Range(0,0.5))=0.1
        _ScaleSize("Scale Size",Range(0,1))=1
        _FadeOut("Fade Out",Range(0,1))=1
        _Contrast("Contrast", Range(0,0.5))=0.2

        _FresnelSize("Fresnel Size",Range(0,2))=0.5
        _FresnelBlur("Fresnel Blur",Range(0,1))=0.5
        _FresnelIntensity("Fresnel Intensity",Range(0,2))=1.5

        _NoiseIntensity("Noise Intensity",Range(0,1))=0.5
        _NoiseLineScale("Noise LineScale",Range(0,1))=0.5
        
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" }
        LOD 100

        Pass
        {
            Blend SrcAlpha One
            //Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            

            #include "UnityCG.cginc"
            #include "../ShaderLibs/UVOperator.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0; 
                float4 vertex : SV_POSITION;
            };

            sampler2D _MapTex;
            float4 _MainColor;
            float4 _MapTex_ST;
            sampler2D _MotionTex;
            float _FlowSpeed;
            float _ScaleSize;
            float _FadeOut;
            float _Contrast;

            float _FresnelSize;
            float _FresnelBlur;
            float _FresnelIntensity;

            float _NoiseIntensity;
            float _NoiseLineScale;
           
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MapTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            


            fixed4 frag (v2f i) : SV_Target
            {
                
                
                float2 uv = i.uv;
                float speed = _Time.y*_FlowSpeed;
                                
                //按不同速度旋转主贴图的RGB通道
                float2 rotR = Rotate2D_Rad(uv,0.5,1*speed);
                float2 rotG = Rotate2D_Rad(uv,0.5,-1*speed);
                float2 rotB = Rotate2D_Rad(uv,0.5,0.5*speed);
                float rot1 = tex2D(_MapTex,rotR).r;
                float rot2 = tex2D(_MapTex,rotG).g;
                float rot3 = tex2D(_MapTex,rotB).b;

                // GB通道的极坐标
                float fadeG = Polar(uv,_FadeOut);
                float fadeB = Polar(-uv,_FadeOut);
                // 极坐标的衰减效果
                rot1*=1-_FadeOut;
                rot2*=fadeG;
                rot3*=fadeB;

                //最终转动的值
                float rot = saturate(rot1+rot2+rot3);
                //模型缩放
                uv-=0.5;
                uv/=_ScaleSize; 
                uv+=0.5;

                

                //闪烁
                float flash = cos(speed+cos(speed*90)*100)*_NoiseIntensity+0.5;
                //垂直滚动噪声线
                float noiseLine =abs(sin(floor(uv.y*128)+speed*20))*_NoiseLineScale;
                
                noiseLine*=flash;
                
                //采样uv移动
                float4 motionUV =tex2D(_MotionTex,uv);
                
                //获取B通道的遮罩
                float uvMask = motionUV.b;
                
                //画圆获得反射形状 
                float circle =1-DrawCircle01(uv,_FresnelSize,_FresnelBlur);
                float fresnel = uvMask * circle * _FresnelIntensity;
                
                //return fresnel;
                //通过G通道 重映射垂直方向上的衰减 
                float fadeout = 1-_FadeOut;
                float fade = Remap(0,1,_FadeOut,1,motionUV.g);

                //获取前半球在X方向上的UV运动 
                float2 frontMotion = float2(motionUV.r,fade);
                frontMotion.x+=speed;
                //以此UV采样主贴图，获取ahpla通道作为前半球uv运动的值
                float uvflow1 =tex2D(_MapTex, frac(frontMotion)).a;
                
                //获取后半球的uv运动
                float2 backMotion = float2(- frac(motionUV.r+0.5), fade); 
                backMotion.x+=speed; 
                float uvflow2 = tex2D(_MapTex, frac(backMotion)).a*_Contrast;
                
                //计算贴图的描边发光
                float growEdge = saturate((1 -uvflow1)*64*uvflow1 );
               
                
                //计算uv运动
                float uvflow = uvflow1+uvflow2+noiseLine+growEdge+fresnel;
                

                uvflow*=fadeout*uvMask;

                uvflow+= rot;
                
                float4 col = float4(_MainColor.rgb,uvflow);

                return col;
            }
            ENDCG
        }
    }
}
